package com.zq.assistant.utils;

import com.zq.assistant.ui.widget.recyclerview.multi.MultiRecyclerView;

/**
 * Created by zhangqiang on 17-4-12.
 */

public class PaginationHelper {

    private Callback callback;

    private int initPageIndex;
    private int page;

    private boolean isRefreshing;

    private boolean isLoadingMore;

    public PaginationHelper(int initPageIndex, Callback callback) {
        this.initPageIndex = initPageIndex;
        this.callback = callback;
    }

    public PaginationHelper(Callback callback) {
        this(0,callback);
    }

    public PaginationHelper() {
        this(0,null);
    }

    public PaginationHelper(int initPageIndex) {
        this(initPageIndex,null);
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public void refresh(){

        if(isRefreshing){
            return;
        }

        if(isLoadingMore && callback != null){
            callback.stopLoad(page);
        }

        isRefreshing = true;

        if(callback != null){
            callback.onLoadData(initPageIndex);
        }
    }

    public void nextPage(){

        if(isLoadingMore){
            return;
        }

        if(isRefreshing && callback != null){
            callback.stopLoad(initPageIndex);
        }

        isLoadingMore = true;

        if(callback != null){

            final int loadPage = page + 1;
            callback.onLoadMoreData(loadPage);
        }
    }

    public void setRefreshSuccess(){

        isRefreshing = false;
        page = initPageIndex;

        if(callback != null){
            callback.onRefreshComplete();
        }
    }

    public void setRefreshFail(){

        isRefreshing = false;
        if(callback != null){
            callback.onRefreshComplete();
        }
    }

    public void setLoadMoreSuccess(){

        isLoadingMore = false;
        page ++;

        if(callback != null){
            callback.onLoadMoreComplete();
        }
    }

    public void setLoadMoreFail(){

        isLoadingMore = false;
        if(callback != null){
            callback.onLoadMoreComplete();
        }
    }

    public interface LoadCallback{

        void onLoadData(int page);

        void onLoadMoreData(int page);

        void stopLoad(int page);
    }

    public interface Callback extends LoadCallback{

        void onRefreshComplete();

        void onLoadMoreComplete();
    }

    public void setupWithMultiRecyclerView(MultiRecyclerView multiRecyclerView, PaginationHelper.LoadCallback callback){

        MultiRecyclerView.OnRefreshListener refreshListener = new MultiRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh(MultiRecyclerView multiRecyclerView) {
                refresh();
            }
        };

        MultiRecyclerView.OnLoadMoreListener loadMoreListener = new MultiRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoadMore(MultiRecyclerView multiRecyclerView) {
                nextPage();
            }
        };

        multiRecyclerView.setOnRefreshListener(refreshListener);
        multiRecyclerView.setOnLoadMoreListener(loadMoreListener);
        setCallback(new InnerCallback(multiRecyclerView,callback));
    }

    private static class InnerCallback implements PaginationHelper.Callback {

        private MultiRecyclerView multiRecyclerView;
        private PaginationHelper.LoadCallback loadCallback;

        InnerCallback(MultiRecyclerView multiRecyclerView, PaginationHelper.LoadCallback loadCallback) {
            this.multiRecyclerView = multiRecyclerView;
            this.loadCallback = loadCallback;
        }

        @Override
        public void onRefreshComplete() {
            multiRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMoreComplete() {
            multiRecyclerView.loadMoreComplete();
        }

        @Override
        public void onLoadData(int page) {
            if(loadCallback != null){
                loadCallback.onLoadData(page);
            }
        }

        @Override
        public void onLoadMoreData(int page) {
            if(loadCallback != null){
                loadCallback.onLoadMoreData(page);
            }
        }

        @Override
        public void stopLoad(int page) {
            if(loadCallback != null){
                loadCallback.stopLoad(page);
            }
        }
    }
}
